﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DesignPatternToolkit.Wizard.Engine;
using System.Reflection;

namespace DesignPatternToolkit.Wizard.WizardPages
{
	public partial class ConfigurationProperties : WizardPageBase
	{
		public ConfigurationProperties()
		{
			InitializeComponent();
		}

		public override void BeforeLoad()
		{
			propertyGrid1.SelectedObject = WizardConfiguration.Instance.PatternBuilder.Configuration;

            SetDefaultProperties();
		}

        private void SetDefaultProperties()
        {
            Type type = propertyGrid1.SelectedObject.GetType();
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                Attribute[] attributes = property.GetCustomAttributes(typeof(DefaultValueAttribute), true) as Attribute[];
                if (attributes != null && attributes.Length > 0)
                {
                    foreach (Attribute attribute in attributes)
                    {
                        DefaultValueAttribute defaultValue = attribute as DefaultValueAttribute;
                        if (defaultValue != null)
                        {
                            property.SetValue(propertyGrid1.SelectedObject, defaultValue.Value, null);
                        }
                    }
                }
            }
        }

	}
}
